package dk.dr.radio.diverse;


/**
 * Kilde: http://www.dr.dk/assets/js/004/dr/dr-geo-location.js
 */
class P4Geokoordinater {

  static class Omraade {
    double[][] koordinater;
    String bynavn;
    String p4kode;

    Omraade(double[][] koordinater, String bynavn, String p4kode) {
      this.koordinater = koordinater;
      this.bynavn = bynavn;
      this.p4kode = p4kode;
    }
  }


  private static double kvadrat(double v) {
    return v * v;
  }


  static P4Geokoordinater.Omraade findNærmesteOmråde(double lat, double lon) {
    P4Geokoordinater.Omraade[] områder = new Omraade[]{
        new Omraade(new double[][]{{55.57019, 11.79519}, {55.677189, 11.74026}, {55.730591, 11.80343}, {55.957272, 11.80137}, {56.226551, 11.61117}, {56.284149, 12.01149}, {56.161991, 12.84164}, {55.57019, 12.85331}, {55.468262, 12.72033}, {55.49091, 12.36511}, {55.37677, 12.09869}, {55.427841, 11.95655}, {55.54689, 11.89476}, {55.57019, 11.79519}}, "København", "KH4"),
        new Omraade(new double[][]{{55.428909, 11.06589}, {55.008339, 11.067352}, {54.751888, 10.8197}, {54.4911, 11.70959}, {54.65667, 12.53356}, {55.274731, 12.68737}, {55.490898, 12.36512}, {55.376759, 12.09868}, {55.42783, 11.95653}, {55.546879, 11.89474}, {55.570179, 11.79517}, {55.524349, 11.59606}, {55.569752, 11.60053}, {55.617519, 11.45874}, {55.549412, 11.30115}, {55.51701, 11.37239}, {55.459728, 11.25412}, {55.428909, 11.06589}}, "Sjælland", "NV4"),
        new Omraade(new double[][]{{56.592758, 9.99618}, {56.69017, 10.20046}, {56.708271, 10.26457}, {56.701859, 10.35873}, {56.773891, 10.6248}, {56.840328, 10.98495}, {56.790649, 11.85241}, {55.716591, 10.70983}, {55.700508, 10.06656}, {55.793751, 10.15926}, {55.823078, 9.94348}, {55.80135, 9.91241}, {55.80246, 9.79512}, {55.908161, 9.58359}, {55.9109, 9.50939}, {56.10957, 9.27658}, {56.10498, 9.36584}, {56.229801, 9.31709}, {56.255486, 9.192166}, {56.301338, 9.203}, {56.370266, 9.402838}, {56.313091, 9.491479}, {56.305664, 9.671418}, {56.567829, 9.747019}, {56.592758, 9.99618}}, "Østjylland", "ÅR4"),
        new Omraade(new double[][]{{54.848152, 14.359131}, {55.50375, 14.36462}, {55.502193, 15.545654}, {54.841827, 15.556641}, {54.848152, 14.359131}}, "Bornholm", "RØ4"),
        new Omraade(new double[][]{{55.881081, 7.82707}, {55.791237, 8.476631}, {55.846409, 8.50547}, {55.849781, 8.63109}, {55.802429, 8.74031}, {55.77309, 8.71284}, {55.71666, 8.79867}, {55.700359, 8.75986}, {55.63652, 8.85154}, {55.584942, 9.03968}, {55.614761, 9.1598}, {55.578339, 9.24156}, {55.49519, 9.24293}, {55.422771, 9.15607}, {55.36945, 9.2171}, {55.295528, 9.09256}, {55.333241, 9.05754}, {55.301399, 8.89755}, {55.245461, 8.90167}, {55.264832, 8.62323}, {55.165531, 7.95547}, {55.881081, 7.82707}}, "Esbjerg", "ES4"),
        new Omraade(new double[][]{{55.715889, 10.70926}, {55.429111, 11.06529}, {55.009121, 11.06597}, {54.751572, 10.81741}, {54.556931, 10.44799}, {55.412987, 9.630461}, {55.453159, 9.70916}, {55.495579, 9.66041}, {55.524738, 9.67346}, {55.512489, 9.74109}, {55.548248, 9.74796}, {55.63361, 10.00099}, {55.699928, 10.06605}, {55.715889, 10.70926}}, "Fyn", "OD4"),
        new Omraade(new double[][]{{55.88147, 7.82775}, {55.778889, 8.48419}, {55.849869, 8.63113}, {55.802704, 8.739839}, {55.899181, 8.8282}, {55.895721, 8.95179}, {55.844479, 8.96209}, {55.845249, 9.15573}, {55.95266, 9.2134}, {55.915352, 9.39399}, {55.910912, 9.50935}, {56.1096, 9.2765}, {56.105099, 9.36567}, {56.22979, 9.31698}, {56.255459, 9.19212}, {56.301392, 9.20293}, {56.370281, 9.40283}, {56.313099, 9.4915}, {56.305679, 9.6714}, {56.567799, 9.74693}, {56.632771, 9.63488}, {56.659809, 9.16603}, {56.685841, 9.14337}, {56.72485, 9.20826}, {56.902248, 9.0232}, {57.02055, 9.09256}, {57.30867, 8.9645}, {56.8979, 7.75543}, {55.88147, 7.82775}}, "Midt & Vest", "HO4"),
        new Omraade(new double[][]{{57.30872, 8.96484}, {57.020451, 9.09281}, {56.902302, 9.02338}, {56.72485, 9.20929}, {56.685459, 9.14371}, {56.66, 9.16637}, {56.632809, 9.63501}, {56.567848, 9.74689}, {56.592861, 9.99592}, {56.69046, 10.20046}, {56.708271, 10.26432}, {56.702, 10.35873}, {56.773979, 10.62429}, {56.840561, 10.98266}, {57.54295, 11.53198}, {57.955669, 10.64575}, {57.727612, 9.40429}, {57.30872, 8.96484}}, "Nordjylland", "ÅL4"),
        new Omraade(new double[][]{{56.084297, 11.101685}, {55.716469, 10.70995}, {55.429008, 11.06597}, {55.459782, 11.25412}, {55.516972, 11.37222}, {55.54932, 11.30089}, {55.617519, 11.45874}, {55.569752, 11.60061}, {55.524349, 11.59606}, {55.57019, 11.79513}, {55.677391, 11.73992}, {55.730492, 11.80292}, {55.785839, 11.8024}, {55.957272, 11.80103}, {56.226368, 11.610146}, {56.084297, 11.101685}}, "Nordvestsjælland", "NS4"),
        new Omraade(new double[][]{{55.164749, 7.9541}, {55.264252, 8.62426}, {55.245361, 8.9018}, {55.301319, 8.89763}, {55.333141, 9.05745}, {55.295441, 9.09247}, {55.36916, 9.21718}, {55.32885, 9.26233}, {55.358028, 9.36103}, {55.30843, 9.42764}, {55.412792, 9.630289}, {55.336369, 9.70436}, {54.556732, 10.447483}, {54.541801, 8.11615}, {55.164749, 7.9541}}, "Syd", "ÅB4"),
        new Omraade(new double[][]{{55.908031, 9.58351}, {55.802429, 9.79499}, {55.801281, 9.91241}, {55.82288, 9.94331}, {55.793751, 10.15857}, {55.633808, 10.0003}, {55.548828, 9.74762}, {55.512489, 9.74075}, {55.525131, 9.67346}, {55.49519, 9.65972}, {55.45335, 9.70848}, {55.308491, 9.42762}, {55.358028, 9.36172}, {55.328911, 9.26233}, {55.422771, 9.1571}, {55.49519, 9.24362}, {55.578339, 9.24224}, {55.614811, 9.15985}, {55.585041, 9.03973}, {55.636589, 8.85167}, {55.70034, 8.75996}, {55.716709, 8.79889}, {55.77317, 8.71297}, {55.899132, 8.8282}, {55.89571, 8.951733}, {55.848526, 8.962768}, {55.848511, 9.06345}, {55.952461, 9.21358}, {55.915329, 9.39399}, {55.908031, 9.58351}}, "Trekanten", "TR4"),
    };


    P4Geokoordinater.Omraade nærmeste = null;
    double mindist2 = Double.MAX_VALUE;
    for (P4Geokoordinater.Omraade omr : områder) {
      double dist2 = Double.MAX_VALUE;
      for (double[] k : omr.koordinater) {
        dist2 = Math.min(dist2, kvadrat(k[0] - lat) + kvadrat(k[1] - lon));
      }
      if (mindist2 > dist2) {
        nærmeste = omr;
        mindist2 = dist2;
      }
    }
    return nærmeste;
  }
}
